package org.stream.answerduel.aspect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.stream.answerduel.dto.Result;
import org.stream.answerduel.service.UserService;
import org.stream.answerduel.util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * @author 郭榕
 * @date 2019/7/20 15:24
 * 用户控制器拦截器,拦截对用户操作的所有请求
 */
@Component
public class UserInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String token = httpServletRequest.getHeader("token");
        System.out.println("从请求头获取的token如下: " + token);

        //Cookie cookie = new Cookie("token",token);
        //httpServletResponse.addCookie(cookie);

        if(token != null && token != "")
        {
            userService.checkToken(token);
            if(JwtUtil.compareTime(token))
            {
                userService.flushToken(token);
                System.out.println("拦截成功,通过放行===================返回true");
                return true;
            }
            else
            {
                HttpSession seesion = httpServletRequest.getSession();
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/api/user/tokenOverdue");
                System.out.println("token过期,请重新登录......");
                System.out.println("拦截成功,token过期,请重新登录===================返回false");
                return false;
            }
        }
        else
        {
            HttpSession seesion = httpServletRequest.getSession();
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/api/user/tokenOverdue");
            System.out.println("未获取到token==========" + token);
            return false;
        }
        //returnJson(httpServletResponse,json.toString());

        //只有这里返回true,下面的postHandle方法才会执行;*/

        //return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("执行完请求的方法后执行此方法===============");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }

    /**
     * @param response
     * @param result
     * @throws Exception
     * 拦截成功，不给通过后返回的数据.
     */
    private void returnJson(HttpServletResponse response, Result result) throws Exception{

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        response.getWriter().print(result);
    }

}